<?php

namespace app\modules\api\controllers;

use app\models\ApiException;
use app\models\Agent;
use app\models\Payment;
use app\models\Refund;
use Yii;
class PaymentController extends BasePublicController{
        //企业付款
	public function actionIndex(){
                $json_raw = file_get_contents("php://input");
                $json_data = json_decode($json_raw,true);
                if(empty($json_data['amount'])){
                        $res['code'] = '1001';
                        $res['msg'] = "参数错误";
                        echo json_encode($res);
                        exit;
                }
                $payment_time = $json_data['payment_time'];
                $partner_trade_no = $json_data['partner_trade_no'];
                $payment_no = $json_data['payment_no'];
                $time = $payment_time[0];
                $day = date('Y-m-d',strtotime($time));
                $today = strtotime($day);
                $pack = Agent::findOne(['username'=>$json_data['name']]);
        	$payment = new Payment();
        	$payment->amount = $json_data['amount'];
                $payment->openid = $json_data['openid'];
                $payment->remark = $json_data['remark'];
                $payment->name 	 = $json_data['name'];
                $payment->bill_id = $json_data['bill_id'];
                $payment->agent_id = $pack['id'];
                $payment->payment_status = 1;
                $payment->partner_trade_no = $partner_trade_no[0];
                $payment->payment_no = $payment_no[0];
                $payment->payment_time = $time;
                $payment->time = $today;
                if($payment->save()){
                        $res['code'] = '1';
                        $res['msg']  = '操作成功';
                        echo json_encode($res);
                        exit;
                }
        
        }
        //退款
        public function actionRefund(){
                $json_raw = file_get_contents("php://input");
                $json_data = json_decode($json_raw,true);
                if(empty($json_data['refund_fee'])){
                        $res['code'] = '1001';
                        $res['msg'] = '退款金额不能为空';
                        echo json_encode($res);
                        exit;
                }
                $out_trade_no = $json_data['out_trade_no'];
                $out_refund_no = $json_data['out_refund_no'];
                $refund_id = $json_data['refund_id'];
                $refund = new Refund();
                $refund->recharge_id = $json_data['recharge_id'];
                $refund->card_id = $json_data['card_id'];
                $refund->package_id = $json_data['package_id'];
                $refund->out_trade_no = $out_trade_no[0];
                $refund->out_refund_no = $out_refund_no[0];
                $refund->refund_id = $refund_id[0];
                $refund->refund_fee = $json_data['refund_fee'];
                $refund->refund_status = 1;
                $refund->source = $json_data['source'];
                $refund->refund_reson = $json_data['refund_reson'];
                $refund->refund_time = time();
                if($refund->save()){
                        $res['code'] = '1';
                        $res['msg'] = '操作成功';
                        echo json_encode($res);
                        exit;
                }

        }

}